package com.netease.ssm.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.netease.ssm.cons.CommonConstant;
import com.netease.ssm.meta.Content;
import com.netease.ssm.meta.Product;
import com.netease.ssm.meta.Trx;
import com.netease.ssm.meta.User;
import com.netease.ssm.service.DeleteService;
import com.netease.ssm.service.EditService;
import com.netease.ssm.service.ProductService;
import com.netease.ssm.service.PublicService;
import com.netease.ssm.service.ShowService;

@Controller
public class ContentController extends BaseController{
	@RequestMapping(value ="/public",method = RequestMethod.GET)
	 private ModelAndView accountPage(HttpServletRequest request,HttpServletResponse respone){
		ModelAndView mav = new ModelAndView();
		User user = (User) request.getSession().getAttribute(CommonConstant.USER_CONTEXT);
		if(user != null && user.getUserType()==1){
			mav.setViewName("public");
		   }else{
			   try {
					respone.sendError(HttpServletResponse.SC_FORBIDDEN, "no permission");
				} catch (IOException e) {
					e.printStackTrace();
				}
			   
		   }
		 return mav;
		}
	@Autowired
	private PublicService publicService;
    @RequestMapping(value = "/publicSubmit",method = RequestMethod.POST)
    public ModelAndView publicSubmit(HttpServletRequest request,Content content){
    	publicService.publicSubmit(content);
    	ModelAndView mav = new ModelAndView();
    	mav.addObject("product",content);
		return mav;
    }
    @Autowired
    private ShowService showService;
    @Autowired
	private ProductService productService;
    @RequestMapping(value ="/show",params="id",method = RequestMethod.GET)
    public ModelAndView showProduct(HttpServletRequest request,@RequestParam(value = "id")int id){
    	Content content = new Content();
    	content = showService.getProductById(id);
    	Trx trx = productService.getTrxByContentId(id);
    	ModelAndView mav = new ModelAndView();
    	Product product = new Product();
    	if(content != null){
    		product.setId(content.getId());
        	product.setTitle(content.getTitle());
        	product.setSummary(content.getSummary());
        	product.setPrice(content.getPrice());
        	product.setImage(new String(content.getImage()));
            if(trx != null){
            	String isBuy = "true";
            	product.setIsBuy(isBuy);
            	product.setBuyPrice(trx.getPrice());
            }
        	try {
    			product.setDetail(new String(content.getDetail(),"UTF-8"));
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	mav.setViewName("show");
    	mav.addObject("product", product);
		return mav;
    	
    }
    @RequestMapping(value ="/edit",params="id",method = RequestMethod.GET)
    public ModelAndView editProduct(HttpServletRequest request,@RequestParam(value = "id")int id,
    		HttpServletResponse response){
    	ModelAndView mav = new ModelAndView();
    	User user = (User) request.getSession().getAttribute(CommonConstant.USER_CONTEXT);
		if(user == null){
			try {
				response.sendRedirect("/ssm/login");
			} catch (IOException e) {
				e.printStackTrace();
			}
		   }else if(user.getUserType()==1){
			    Content content = new Content();
		    	content = showService.getProductById(id);
		    	Product product = new Product();
		    	product.setId(content.getId());
		    	product.setTitle(content.getTitle());
		    	product.setSummary(content.getSummary());
		    	product.setPrice(content.getPrice());
		    	product.setImage(new String(content.getImage()));
		    	product.setDetail(new String(content.getDetail()));
		    	mav.addObject("product", product);  
		   }
    	mav.setViewName("edit");
    	return mav;
    }
    @Autowired
    private EditService editService;
    @RequestMapping(value ="/editSubmit",params="id",method = RequestMethod.POST)
    public ModelAndView editSubmit(HttpServletRequest request,@RequestParam(value = "id")int id,Content content){
    	editService.editSubmit(content);
    	ModelAndView mav = new ModelAndView();
    	mav.addObject("product",content);
		return mav; 	
    }
    @Autowired
    private DeleteService deleteService;
    @RequestMapping(value = "/api/delete",params="id",method = RequestMethod.POST)
    public ModelAndView editSubmit(HttpServletRequest request,@RequestParam(value = "id")int id){
    	ModelAndView mav = new ModelAndView();
    	if(id != 0){
    		 deleteService.deleteProduct(id);
    		 mav.addObject("code", 200);
			 mav.addObject("message", "success");
			 mav.addObject("result", true);
    	}else{
    		 mav.addObject("code", 401);
			 mav.addObject("message", "faile");
			 mav.addObject("result", false);
    	}
		return mav;
    }
    
}
